package com.sinosoft.perfect.system._authorization.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TResourceSystem;
import com.sinosoft.perfect.system._.po.TRoleSystem;
import com.sinosoft.perfect.system._authorization.dataFormat.IAuthorizationTreeDataFormat;
import com.sinosoft.perfect.system._authorization.service.IRoleResourceAuthorizeService;
import com.sinosoft.perfect.system._authorization.vo.RoleResourceRelationAuthorize;
import com.sinosoft.perfect.system._resource.service.IResourceService;
import com.sinosoft.perfect.system._roleSystem.service.IRoleSystemService;

@SuppressWarnings("serial")
public class RoleResourceBindingAction extends BaseAction {

	private TRoleSystem troleSystem;

	private IRoleSystemService roleSystemService;

	private IRoleResourceAuthorizeService iroleResourceAuthorizeService;

	private IResourceService iresourceService;

	private IAuthorizationTreeDataFormat roleSystemResourceTreeDataFormat;

	private String roleSystemId;

	private String resourceId;

	private RoleResourceRelationAuthorize roleResourceRelationAuthorize;

	public String roleSystemQuery() {
		try {
			List<TRoleSystem> roleSystems = roleSystemService.selectByTRoleSystem(troleSystem);
			request.setAttribute("roleSystems", roleSystems);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "RRB_roleSystemQueryList";
	}

	public String initResourceTree() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			TResourceSystem tresourcesystem = new TResourceSystem();
			tresourcesystem.setStatus("1");
			List<TResourceSystem> resources = iresourceService.selectTResource(tresourcesystem);
			List<String> resourceIds = iroleResourceAuthorizeService.queryResourceIdsOfRole(roleSystemId);
			resultXml = roleSystemResourceTreeDataFormat.dataFormatRoleSystemResourceTree(resources, resourceIds);

		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String roleSystemResourceInfo() {
		try {
			RoleResourceRelationAuthorize roleResourceRelationAuthorize = iroleResourceAuthorizeService.queryTRoleResourceRelationByResourceAndRole(resourceId, roleSystemId);
			request.setAttribute("RelationAuthorize", roleResourceRelationAuthorize);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "roleSystem_resource_button";
	}

	public String updatePowerResourceOfRole() {
		try {
			iroleResourceAuthorizeService.updatePowerOfRoleResource(roleResourceRelationAuthorize);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		this.resourceId = roleResourceRelationAuthorize.getResourceId();
		this.roleSystemId = roleResourceRelationAuthorize.getRoleSystemId();
		request.setAttribute("operateWin", "operateWin");
		return roleSystemResourceInfo();
	}

	public TRoleSystem getTroleSystem() {
		return troleSystem;
	}

	public void setTroleSystem(TRoleSystem troleSystem) {
		this.troleSystem = troleSystem;
	}

	public void setIroleResourceAuthorizeService(IRoleResourceAuthorizeService iroleResourceAuthorizeService) {
		this.iroleResourceAuthorizeService = iroleResourceAuthorizeService;
	}

	public void setIresourceService(IResourceService iresourceService) {
		this.iresourceService = iresourceService;
	}

	public String getRoleSystemId() {
		return roleSystemId;
	}

	public void setRoleSystemId(String roleSystemId) {
		this.roleSystemId = roleSystemId;
	}

	public String getResourceId() {
		return resourceId;
	}

	public void setResourceId(String resourceId) {
		this.resourceId = resourceId;
	}

	public RoleResourceRelationAuthorize getRoleResourceRelationAuthorize() {
		return roleResourceRelationAuthorize;
	}

	public void setRoleResourceRelationAuthorize(RoleResourceRelationAuthorize roleResourceRelationAuthorize) {
		this.roleResourceRelationAuthorize = roleResourceRelationAuthorize;
	}

	public void setRoleSystemService(IRoleSystemService roleSystemService) {
		this.roleSystemService = roleSystemService;
	}

	public void setRoleSystemResourceTreeDataFormat(IAuthorizationTreeDataFormat roleSystemResourceTreeDataFormat) {
		this.roleSystemResourceTreeDataFormat = roleSystemResourceTreeDataFormat;
	}
}
